/*
 * @Author: Janssen000908 2601566046@qq.com
 * @Date: 2025-03-26 16:04:47
 * @LastEditors: Janssen000908 2601566046@qq.com
 * @LastEditTime: 2025-03-26 18:18:54
 * @FilePath: /linux_-demo/learn26/pipe/testPipe.cpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%
 */
#include <iostream>
#include <unistd.h> //管道文件
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>


// #define N 2
// #define NUM 1024 //文件缓冲区大小

// using namespace std;


// void Write(int wfd)
// {
//     string s = "hello , i am child";
//     pid_t child_pid = getpid();
//     int num = 0;

//     char buffer[NUM];
//     while(true)
//     {
//         buffer[0] = 0;//字符串清空，数组作为字符串
        
//         //1.直接转化为字符串
//         //2.snprintf(字符串格式化)
//         //int snprintf(char *str, size_t size, const char *format, ...);
//         snprintf(buffer,sizeof(buffer),"%s-%d-%d",s.c_str(),child_pid,num);
//         write(wfd,buffer,strlen(buffer));//文件写入
//         //cout << "child_write[" << getpid() << "]: " << buffer << endl;

//         sleep(1);
//     }
// }

// void Read(int rfd)
// {
//     const char* buf[NUM];
//     while(true)
//     {
//         buf[0] = 0;
//         ssize_t n = read(rfd,buf,sizeof(buf));
//         if(n > 0)
//         {
//             buf[n] = '\0'; // '\0',代表读出buf是一个字符串
//             cout << "father_get msg:[" << getpid() << "] :" << buf << endl;
//         }
//     }
// }
// int main()
// {
//     //创建匿名管道
//     int pipefd[N] = {0};
//     int n = pipe(pipefd);
//     if(n < 0)
//     {
//         return 1;//管道返回错误
//     }
//     // cout << "pipefd[0] = " << pipefd[0] << " ";
//     // cout << "pipefd[1] = " << pipefd[1] << endl;
    
//     //建立单项信道
//     pid_t id = fork();
//     if(id < 0)
//     {
//         return 2;
//     }
//     if(id == 0)
//     {
//         //子进程
//         close(pipefd[0]);//关闭读
//         Write(pipefd[1]);



//         close(pipefd[1]);//关闭写
//         exit(0);
        
//     }

//     //父进程
//     close(pipefd[1]);//关闭写

//     Read(pipefd[0]);
//     pid_t rid = waitpid(id,nullptr,0);
//     if(rid < 0)
//     {
//         return 3;
//     }


//     close(pipefd[0]);

//     return 0;
// }

using namespace std;
int main()
{
    int pipefd[2] = {0};
    int n = pipe(pipefd);
    if(n < 0) return 1;

    pid_t id = fork();
    if(id < 0) return 2;
    if(id == 0)
    {
        //child_write;father_read
        //child
        close(pipefd[0]);
        char buffer[1024];
        string s = "test_pipe";
        pid_t id = getpid();
        int num = 0;
        while (1)
        {
            buffer[0] = 0;
            snprintf(buffer,sizeof(buffer),"%s-%d-%d",s.c_str(),id,num++);  
            
            write(pipefd[1],buffer,strlen(buffer));
            sleep(1);
        }
        close(pipefd[1]);
        exit(0);
    }

    //father
    close(pipefd[1]);

    while (1)
    {
        char buffer[1024];
        read(pipefd[0],buffer,sizeof(buffer));
        buffer[sizeof(buffer) - 1] = 0;
        cout << "father:" << buffer << endl;

        //sleep(1);
    }
    
    pid_t ret = waitpid(id,nullptr,0);
    if(ret < 0)
    {
        return 3;
    }
    close(pipefd[0]);
    return 0;
}
